/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.far.aus.adm;

import com.far.aus.adm.clas.Auspicio;
import com.far.lib.BaseDatos;
import com.far.lib.DatosDinamicos;
import com.far.lib.Fecha;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jorge
 */
public class FrmAuspicioVer extends HttpServlet {

    private String _ip = null;
    private int _puerto = 1433;
    private String _db = null;
    private String _usuario = null;
    private String _clave = null;
    
    private String plan_ip = null;
    private int plan_puerto = 1433;
    private String plan_db = null;
    private String plan_usuario = null;
    private String plan_clave = null;
    
    public void init(ServletConfig config) throws ServletException
    {
        this._ip = config.getServletContext().getInitParameter("_IP");
        this._puerto = Integer.parseInt(config.getServletContext().getInitParameter("_PUERTO"));
        this._db = config.getServletContext().getInitParameter("_DB");
        this._usuario = config.getServletContext().getInitParameter("_USUARIO");
        this._clave = config.getServletContext().getInitParameter("_CLAVE");
        
        this.plan_ip = config.getServletContext().getInitParameter("PLAN_IP");
        this.plan_puerto = Integer.parseInt(config.getServletContext().getInitParameter("PLAN_PUERTO"));
        this.plan_db = config.getServletContext().getInitParameter("PLAN_DB");
        this.plan_usuario = config.getServletContext().getInitParameter("PLAN_USUARIO");
        this.plan_clave = config.getServletContext().getInitParameter("PLAN_CLAVE");
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Mon, 01 Jan 2001 00:00:01 GMT");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Cache-Control", "must-revalidate");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id");
        
        BaseDatos objPlanMercadeo = new BaseDatos(this.plan_ip, this.plan_puerto, this.plan_db, this.plan_usuario, this.plan_clave);
        Auspicio objAuspicio = new Auspicio(this._ip, this._puerto, this._db, this._usuario, this._clave);
        
        String msg = "";
        String num_auspicio = "";
	String fecha_creacion = Fecha.getFecha("SQL");
	String tipo_plan = "";
	String proveedor = "";
	String cedula = "";
	String contacto = "";
	String correo = "";
	String subtotal = "";
	String iva = "";
	String total = "";
	String dias_para_vencimiento = "";
	String observacion = "";
	String adjunto = "";
        if(id.compareTo("-1")!=0){
            try{
                ResultSet rs = objAuspicio.getAuspicio(id);
                if(rs.next()){
                    num_auspicio = rs.getString("num_auspicio")!=null ? rs.getString("num_auspicio") : "";
                    fecha_creacion = rs.getString("txt_fecha_creacion")!=null ? rs.getString("txt_fecha_creacion") : "";
                    tipo_plan = rs.getString("tipo_plan")!=null ? rs.getString("tipo_plan") : "";
                    proveedor = rs.getString("proveedor")!=null ? rs.getString("proveedor") : "";
                    cedula = rs.getString("cedula")!=null ? rs.getString("cedula") : "";
                    contacto = rs.getString("contacto")!=null ? rs.getString("contacto") : "";
                    correo = rs.getString("correo")!=null ? rs.getString("correo") : "";
                    subtotal = rs.getString("subtotal")!=null ? rs.getString("subtotal") : "";
                    iva = rs.getString("iva")!=null ? rs.getString("iva") : "";
                    total = rs.getString("total")!=null ? rs.getString("total") : "";
                    dias_para_vencimiento = rs.getString("dias_para_vencimiento")!=null ? rs.getString("dias_para_vencimiento") : "";
                    observacion = rs.getString("observacion")!=null ? rs.getString("observacion") : "";
                    adjunto = rs.getString("adjunto")!=null ? rs.getString("adjunto") : "";
                    rs.close();
                }
            }catch(Exception e){
                msg = e.getMessage();
            }
        }
        
        ResultSet rsPlanes = objPlanMercadeo.consulta("select id_tipo_plan, nombre from tbl_tipo_plan where estado=1 order by nombre");
        
        try {
            out.print("obj»vta1_html^foc»proveedor^msg»"+msg+"^frm»");
            out.print("<table>");
            out.print("<tr>");
            out.print("<td>Tipo de Plan de Mercadeo:</td>");
            out.print("<td>"+tipo_plan+"</td>");
            out.print("<td nowrap> &nbsp;&nbsp; Nro. de Auspicio: "+num_auspicio
                    + " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fecha de creai&oacute;n: "+fecha_creacion+"</td>");
            out.print("</tr>");
            
            out.print("<tr>");
            out.print("<td>Proveedor: </td>");
            out.print("<td colspan=\"2\">"+proveedor+"</td>");
            out.print("</tr>");
            
            out.print("<tr>");
            out.print("<td>Contacto: </td>");
            out.print("<td>"+contacto+"</td>");
            out.print("<td> &nbsp;&nbsp; C&eacute;dula: "+cedula+"</td>");
            out.print("</tr>");
            
            out.print("<tr>");
            out.print("<td>Correo: </td>");
            out.print("<td colspan=\"2\">"+correo+"</td>");
            out.print("</tr>");
            
            out.print("<tr><td colspan=\"3\">");
                out.print("<fieldset><legend>Plan de Actividades Promocionales</legend>");
                
                
                
                out.print("<table>");
                    out.print("<tr>");
                        out.print("<th style=\"width:160px;\">Plan de Actividades</th>");
                        out.print("<th style=\"width:100px;\">Desde</th>");
                        out.print("<th style=\"width:100px;\">Hasta</th>");
                        out.print("<th style=\"width:60px;\">Graba IVA</th>");
                        out.print("<th style=\"width:70px;\">Sub-total</th>");
                        out.print("<th style=\"width:70px;\">IVA</th>");
                        out.print("<th style=\"width:70px;\">Total</th>");
                        out.print("<th style=\"width:120px;\">Forma de pago</th>");
                    out.print("</tr>");
                
                try{
                    ResultSet rsActividades = objAuspicio.getPlanActividades(id);
                    StringBuilder html = new StringBuilder();
                    int i=0;
                    String plan_actividad;
                    String fecha_desde;
                    String fecha_hasta;
                    String graba_iva;
                    String asubtotal;
                    String aiva;
                    String atotal;
                    String forma_pago;
                    while(rsActividades.next()){
                        plan_actividad = rsActividades.getString("plan_actividad")!=null ? rsActividades.getString("plan_actividad") : "";
                        fecha_desde = rsActividades.getString("txt_fecha_desde")!=null ? rsActividades.getString("txt_fecha_desde") : "";
                        fecha_hasta = rsActividades.getString("txt_fecha_hasta")!=null ? rsActividades.getString("txt_fecha_hasta") : "";
                        graba_iva = rsActividades.getString("graba_iva")!=null ? rsActividades.getString("graba_iva") : "";
                        asubtotal = rsActividades.getString("subtotal")!=null ? rsActividades.getString("subtotal") : "";
                        aiva = rsActividades.getString("iva")!=null ? rsActividades.getString("iva") : "";
                        atotal = rsActividades.getString("total")!=null ? rsActividades.getString("total") : "";
                        forma_pago = rsActividades.getString("forma_pago")!=null ? rsActividades.getString("forma_pago") : "";
                        html.append("<tr>");
                        html.append("<td>"+plan_actividad+"</td>");
                        html.append("<td>"+fecha_desde+"</td>");
                        html.append("<td>"+fecha_hasta+"</td>");
                        html.append("<td align='center'>"+ graba_iva+"</td>");
                        html.append("<td align='right'>"+asubtotal+"</td>");
                        html.append("<td align='right'>"+aiva+"</td>");
                        html.append("<td align='right'>"+atotal+"</td>");
                        html.append("<td> &nbsp; "+forma_pago+"</td>");
                        html.append("</tr>");
                        i++;
                    }
                    out.print(html);
                }catch(Exception e){
                    e.printStackTrace();
                }
                
                    out.print("<tr>");
                        out.print("<td colspan=\"4\" align='right'>Total &nbsp;&nbsp;&nbsp; </td>");
                        out.print("<td align='right'>"+subtotal+"</td>");
                        out.print("<td align='right'>"+iva+"</td>");
                        out.print("<td align='right'>"+total+"</td>");
                        out.print("<td>&nbsp;</td>");
                    out.print("</tr>");
                out.print("</table>");
                
                
                
                
                out.print("</fieldset>");
            out.print("</td></tr>");
            
            out.print("<tr>");
            out.print("<td>Fecha de Pago: </td>");
            out.print("<td>"+dias_para_vencimiento+" D&iacute;as</td>");
            out.print("<td>Observaciones: <br /> "+observacion+"</td>");
            out.print("</tr>");
            
            
            /*out.print("<tr>");
            out.print("<td id=\"ax_adjunto\"></td>");
            out.print("<td colspan=\"2\" align=\"right\">");
            if(id.compareTo("-1")!=0){
                out.print(" &nbsp; <input type=\"button\" value=\"Imprimir\" onclick=\"\" />");
            }
            out.print("</td>");
            out.print("</tr>");*/
            out.print("</table>");
        } finally {
            objPlanMercadeo.cerrar();
            objAuspicio.cerrar();
            out.close();
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
